var app = getApp();
var utils = require('../../utils/shop/formatShop.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {

    //页面标签信息
    navlist: [
      {
        Order_Status: 0,
        nav_name: "全部"
      },
      {
        Order_Status: 1,
        nav_name: "待付款"
      },
      {
        Order_Status: 2,
        nav_name: "待发货"
      },
      {
        Order_Status: 3,
        nav_name: "待收货"
      },
      {
        Order_Status: 4,
        nav_name: "已完成"
      }
    ],

    //订单状态描述
    status_desc: {
      '0': '等待商家确认',
      '1': '待付款',
      '2': '等待商家发货',
      '3': '待收货',
      '4': '已完成',
      '5': '退款中...',
      '6': '换货订单',
      '7': '维修订单',
      //拼团状态
      '20': '等待拼团成功',
      '21': '拼团成功，待发货',
      '24': '拼团失败',
      '31': '待收货',
      '41': '已完成',
      '44': '拼团失败'
    },
    back_status_desc: {
      '0': '退款申请中',
      '1': '商家同意退货,待发货',
      '2': '退货已发货', // 自己发货
      '3': '商家已确认,待退款',
      '4': '退款已完成',
      '5': '商家驳回',
      '6': '不同意退款金额', // 自己不同意
      '7': '金额退回中...', // 商家退款给用户，过度状态
    },

    //订单列表
    orderlist: [],

    //获取订单列表请求的参数
    Order_Status: 0,  //当前显示的订单状态 0：全部  1：待付款  2：待发货  3：待收货
    orderArgs: {
      User_ID: 0,
      Order_Type: 'shop',   //shop、pintuan
      act: 'get_order',
      pageSize: 10,
      page: 1
    },
    //是否能获取订单数据状态  
    get_flag: true,

    //加载更多动画
    loading: true,

    //执行操作动作和订单id
    order_action: null,   //del、pay、confirm
    action_order_id: 0
  },
  //订单评论
  order_commit: function (e) {
    var order_id = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '../pinglun/pinglun?Order_Type=' + this.data.orderArgs.Order_Type + '&Order_ID=' + order_id,
    })
  },
  //菜单切换
  switchNav: function (e) {
    //获取点击的菜单对应的订单状态
    var Order_Status = e.target.dataset.id;
    this.setData({
      Order_Status: Order_Status
    });
    //重新加载页面
    this.load();
  },

  //删除订单
  delOrder: function (e) {
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '确认删除此订单吗？',
      success: function (res) {
        if (res.confirm) {  //确认删除
          wx.showLoading({
            title: '删除订单中...'
          });
          that.setData({
            order_action: 'del',
            action_order_id: order_id
          });
          //删除订单所需参数
          var delArgs = {
            UsersID: app.globalData.userInfo.Users_ID,
            User_ID: app.globalData.userInfo.User_ID,
            action: 'del_order',
            orderid: order_id,
            type: 'cart'
          };
          app.http_req(delArgs, app.globalData.init.api_url, 'POST', that.delOrder_callback);
        }
      }
    });
  },

  //删除订单回调
  delOrder_callback: function (res) {
    wx.hideLoading();
    if (res.errorCode == 0) {
      //删除订单列表中的该订单数据
      var orderlist = this.data.orderlist;
      for (var i in orderlist) {
        if (orderlist[i]['Order_ID'] == this.data.action_order_id) {
          orderlist.splice(i, 1);
        }
      }
      //重置删除订单id
      this.setData({
        orderlist: orderlist,
        order_action: null,
        action_order_id: 0
      });
      wx.showToast({
        title: '删除成功',
        icon: 'success',
        duration: 1000
      });

    } else {
      this.setData({
        order_action: null,
        action_order_id: 0
      });
      wx.showModal({
        title: '错误',
        content: res.msg,
        showCancel: false
      })
    }
  },

  //支付订单
  payOrder: function (e) {
    var order_id = e.currentTarget.dataset.orderid;
    this.setData({
      order_action: 'pay',
      action_order_id: order_id
    });
    app.orderAction('pay_orderlist', { Order_ID: order_id, Order_Type: this.data.orderArgs.Order_Type });
  },

  //确认收货
  confirm_recieve: function (e) {
    var order_id = e.currentTarget.dataset.orderid;
    this.setData({
      order_action: 'confirm',
      action_order_id: order_id
    });
    app.orderAction('confirm', { Order_ID: order_id, Order_Type: this.data.orderArgs.Order_Type });
  },

  //获取订单列表回调
  getOrderList: function () {
    var orderArgs = this.data.orderArgs;
    if (this.data.Order_Status == 0) {  //全部订单时不需传订单状态
      for (var i in orderArgs) {
        if (i == 'Order_Status') {
          delete orderArgs.Order_Status;
        }
      }
    } else {
      orderArgs.Order_Status = this.data.Order_Status;
    }

    this.setData({
      loading: true
    });
    app.http_req(orderArgs, app.globalData.init.api_url, 'POST', res => {
      wx.hideLoading();
      if (res.errorCode == 0) {
        var old_orderlist = this.data.orderlist;
        var orderlist = utils.formatOrder(app.globalData.init.front_url, res.data);
        this.setData({
          orderlist: old_orderlist.concat(orderlist),
          'orderArgs.page': this.data.orderArgs.page + 1,
          get_flag: (res.totalCount / this.data.orderArgs.pageSize) > this.data.orderArgs.page ? true : false
        });
      } else if (res.errorCode == 1) {  //errorCode == 2 无订单
        wx.showModal({
          title: '提示',
          content: typeof res.msg !== 'undefined' ? res.msg : '查询失败',
          showCancel: false
        });
      }

      // 结束下拉刷新
      utils.overPullDownRefresh();

      this.setData({
        loading: false
      });
    });
  },

  //页面加载
  load: function () {
   
    //避免第一次重复加载
    var show_order_status_flag = '0,1,2,3,4'.indexOf(app.globalData.show_order_status) == -1 ? false : true;
    wx.showLoading({
      title: '加载中...'
    });
    app.globalData.order_type = null;
    //初始化数据
    this.setData({
      orderlist: [],
      'orderArgs.User_ID': app.globalData.userInfo && app.globalData.userInfo.User_ID ? app.globalData.userInfo.User_ID : 0,
      'orderArgs.page': 1,
      get_flag: false
    });
    //如果跳转到指定页面
    if (show_order_status_flag) {
      this.setData({
        Order_Status: app.globalData.show_order_status
      });
      app.globalData.show_order_status = -1;
    }

    //获取订单列表
    this.getOrderList();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...'
    });    
    if (options.order_status) {
      app.globalData.show_order_status = options.order_status;
    }
    if (options.order_type && 'shop,pintuan,kanjia'.indexOf(options.order_type) > -1) {
      this.setData({
        'orderArgs.Order_Type': options.order_type
      });
      //更改标题
      let title = '订单列表';
      switch (options.order_type) {
        case 'shop':
          title = '商城订单';
          break;
        case 'pintuan':
          title = '拼团订单';
          break;
        case 'kanjia':
          title = '砍价订单';
          break;
      }
      app.setNavBarTitle(title);
    }
    if (options.type) {
      this.setData({
        Order_Status: options.type
      })
    }
    this.load();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //检查用户是否登录
    if (!app.globalData.userInfo) {
      app.login('', this.load, 'orderlist');
      return false;
    }
    this.load();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //显示加载导航条
    wx.showNavigationBarLoading();
    //初始化数据 (防止数据重复)
    this.setData({
      //订单列表
      orderlist: [],

      //获取订单列表请求的参数
      'orderArgs.page': 1, //shop、pintuan
      //是否能获取订单数据状态  
      get_flag: true,

      //加载更多动画
      loading: true,

      //执行操作动作和订单id
      order_action: null, //del、pay、confirm
      action_order_id: 0
    });
    this.load();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.get_flag && !this.data.loading) {
      this.getOrderList();
    }
  }
})